package com.tengju.user.domain.model.user;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;

@Getter
@AllArgsConstructor
public enum StarLevelEnum {
    NONE(0, StarRoleEnum.NONE, "经销商", "无星", "经销商", "普通用户"),
    CITY_STAR_ONE(10, StarRoleEnum.SERVICE_PROVIDER, "城市服务商1星", "城市服务商1星", "1星", "城市服务商1星"),
    CITY_STAR_TWO(20, StarRoleEnum.SERVICE_PROVIDER, "城市服务商2星", "城市服务商2星", "2星", "城市服务商2星"),
    CITY_STAR_THREE(30, StarRoleEnum.SERVICE_PROVIDER, "城市服务商3星", "城市服务商3星", "3星", "城市服务商3星"),
    CITY_STAR_FOUR(40, StarRoleEnum.SERVICE_PROVIDER, "城市服务商4星", "城市服务商4星", "4星", "城市服务商4星"),
    EC(50, StarRoleEnum.OPERATOR, "大区运营商EC", "大区运营商", "EC", "大区运营商EC"),
    SEC(60, StarRoleEnum.OPERATOR, "大区运营商SEC", "大区运营商", "SEC", "大区运营商SEC"),
    EEC(70, StarRoleEnum.OPERATOR, "大区运营商EEC", "大区运营商", "EEC", "大区运营商EEC");

    private Integer code;
    private StarRoleEnum starRoleEnum;
    private String desc;
    private String showName;
    private String shortName;
    private String userOrientation;

    public boolean isServiceProvider(StarLevelEnum starLevel) {
        return StarLevelEnum.CITY_STAR_ONE == starLevel ||
                StarLevelEnum.CITY_STAR_TWO == starLevel ||
                StarLevelEnum.CITY_STAR_THREE == starLevel ||
                StarLevelEnum.CITY_STAR_FOUR == starLevel;
    }

    public static List<StarLevelEnum> serviceLevel() {
        return List.of(CITY_STAR_ONE,CITY_STAR_TWO,CITY_STAR_THREE,CITY_STAR_FOUR,EC,SEC,EEC);
    }

    public boolean isDealer(StarLevelEnum starLevel) {
        return StarLevelEnum.NONE == starLevel;
    }


    public static StarRoleEnum getStarRoleCodeByLevel(Integer starLevel) {
        StarLevelEnum starLevelEnum = parseByCode(starLevel);
        if (starLevelEnum == null) {
            return null;
        }
        return starLevelEnum.getStarRoleEnum();
    }

    public static StarLevelEnum parseByCode(Integer code) {
        if (code == null) {
            return null;
        }
        return Arrays.stream(StarLevelEnum.values())
                .filter(k -> k.getCode().equals(code))
                .findFirst().orElse(null);
    }

    /**
     * 不管服务商还是运营商只要不是个人身份都可以
     *
     * @param targetStarLevel
     * @param accountType
     * @return
     */
    public static boolean isMatchAccountType(Integer targetStarLevel, Integer accountType) {
        if (targetStarLevel == null || accountType == null) {
            return false;
        }
        StarRoleEnum starRoleEnum = getStarRoleCodeByLevel(targetStarLevel);
        if (starRoleEnum == null) {
            return false;
        }
        return starRoleEnum.isMatchAccountType(accountType);
    }

    /**
     * 是否升星
     *
     * @return
     */
    public static boolean isRise(Integer curStarLevel, Integer targetStarLevel) {
        return targetStarLevel > curStarLevel;
    }

    public boolean isHigher(StarLevelEnum starLevel) {
        if (starLevel == null) {
            return false;
        }
        return this.getCode() > starLevel.getCode();
    }

}
